package org.shan.security.browser;

import org.shan.security.core.authorize.AuthorizeConfigProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.context.HttpRequestResponseHolder;
import org.springframework.security.web.context.SecurityContextRepository;
import org.springframework.social.security.SpringSocialConfigurer;

import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by amanda.shan on 2019/3/18.
 */
@EnableWebSecurity(debug = false)
public class BrowserSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private Set<AuthorizeConfigProvider> providers;

    @Autowired
    private SpringSocialConfigurer imoocSocialSecurityConfig;

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        for (AuthorizeConfigProvider provider : providers) {
            provider.config(http.authorizeRequests());
        }

        http
                .apply(imoocSocialSecurityConfig)
                .and()
                .authorizeRequests()
                .antMatchers(
                        "/auth/*", "/user/login", "/user/regist", "/user/signup", "/social/user",
                        "authentication/form", "/session/invalid"
                ).permitAll()

                .anyRequest().authenticated()
                .and()
                .formLogin()
                .loginPage("/user/login")
                .loginProcessingUrl("/authentication/form")

                .and()
                .sessionManagement()
//                .invalidSessionUrl("/session/invalid")
//                // 配置地址或则策略类
//                .maximumSessions(1) //限制同一个用户只能有一个session登录
//                .expiredSessionStrategy(new MySessionInformationExpiredStrategy())  // 失效后的策略。定制型更高，失效前的请求还能拿
//                .and()

                .and()
                .securityContext()
                .securityContextRepository(new SecurityContextRepository() {
                    @Override
                    public SecurityContext loadContext(HttpRequestResponseHolder requestResponseHolder) {
                        return null;
                    }

                    @Override
                    public void saveContext(SecurityContext context, HttpServletRequest request, HttpServletResponse
                            response) {

                    }

                    @Override
                    public boolean containsContext(HttpServletRequest request) {
                        return false;
                    }
                })

                .and()
                .csrf().disable()//关闭csrd拦截
        ;
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        super.configure(web);

        web.ignoring().antMatchers("/**/*.js", "/**/*.ico", "/**/*.css", "/**/*.jpg", "/**/*.png", "/**/*.woff2",
                "/code/image");

    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
}
